package com.atguigu.study.config;

import com.atguigu.study.service.FunctionAssistant;
import com.atguigu.study.service.InvoiceHandler;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.service.AiServices;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Taeyang
 * @create 2025-03-12 23:23
 */
@Configuration(proxyBeanMethods = false)
public class ChatModelConfig {
    @Bean
    public ChatModel chatModel() {
        return OpenAiChatModel.builder()
            .apiKey(System.getenv("LANGCHAIN4J_KEY"))
            .modelName("qwen-turbo")
            .baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
            .build();
    }

    // @Bean
    // public FunctionAssistant functionAssistant(ChatModel chatModel) {
    //     // 工具说明 ToolSpecification
    //     ToolSpecification toolSpecification = ToolSpecification.builder()
    //         .name("invoice_assistant")
    //         .description("根据用户提交的开票信息，开具发票")
    //         .parameters(JsonObjectSchema.builder()
    //             .addStringProperty("companyName", "公司名称")
    //             .addStringProperty("dutyNumber", "税号")
    //             .addStringProperty("amount", "金额，保留两位有效数字")
    //             .build())
    //         .build();
    //
    //     // 业务逻辑 ToolExecutor
    //     ToolExecutor toolExecutor = (toolExecutionRequest, memoryId) -> {
    //         System.out.println(toolExecutionRequest.id());
    //         System.out.println(toolExecutionRequest.name());
    //         String arguments1 = toolExecutionRequest.arguments();
    //         System.out.println("arguments1 =>>>> " + arguments1);
    //         return "开具成功";
    //     };
    //
    //     return AiServices.builder(FunctionAssistant.class)
    //         .chatModel(chatModel)
    //         .tools(Map.of(toolSpecification, toolExecutor))
    //         .build();
    // }

    @Bean
    public FunctionAssistant functionAssistant(ChatModel chatModel) {

        return AiServices.builder(FunctionAssistant.class)
            .chatModel(chatModel)
            .tools(new InvoiceHandler())
            .build();
    }
}

